package com.liao.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liao.seckill.mapper.OrderMapper;
import com.liao.seckill.pojo.Order;
import com.liao.seckill.pojo.SeckillGoods;
import com.liao.seckill.pojo.SeckillOrder;
import com.liao.seckill.pojo.User;
import com.liao.seckill.service.OrderService;
import com.liao.seckill.service.SeckillGoodsService;
import com.liao.seckill.service.SeckillOrderService;
import com.liao.seckill.util.MD5Util;
import com.liao.seckill.util.UUIDUtil;
import com.liao.seckill.vo.GoodsVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private SeckillGoodsService seckillGoodsService;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private SeckillOrderService seckillOrderService;

    @Resource
    private RedisTemplate redisTemplate;


    @Transactional
    @Override
    public Order seckill(User user, GoodsVo goodsVo) {
        SeckillGoods seckillGoods = seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>().eq("goods_id", goodsVo.getId()));
        seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);
        //seckillGoodsService.updateById(seckillGoods);

//        boolean update = seckillGoodsService.update(new UpdateWrapper<SeckillGoods>().setSql("stock_count=stock_count-1").eq("goods_id", goodsVo.getId()).gt("stock_count", 0));
//
//        if(!update){
//            return null;
//        }

        boolean update = seckillGoodsService.update
                (new UpdateWrapper<SeckillGoods>().setSql("stock_count =stock_count-1")
                        .eq("goods_id", goodsVo.getId()).gt("stock_count", 0));
        if (!update) {//如果更新失败,说明已经没有库存了
             return null;
        }

        Order order = new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goodsVo.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsCount(1);
        order.setGoodsName(goodsVo.getGoodsName());
        order.setGoodsPrice(seckillGoods.getSeckillPrice());
        order.setOrderChannel(1);
        order.setStatus(0);
        order.setCreateDate(new Date());
        orderMapper.insert(order);

        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setGoodsId(goodsVo.getId());
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setUserId(user.getId());

        seckillOrderService.save(seckillOrder);

        // redisTemplate.opsForValue().set("order:" + user.getId() + ":" +goodsVo.getId(),seckillOrder);
        redisTemplate.opsForValue(). set("order:" + user.getId() + ":" + goodsVo.getId(), seckillOrder);

        return order;
    }

    @Override
    public String createPath(User user, Long goodsId) {
        String path = MD5Util.md5(UUIDUtil.uuid());
        redisTemplate.opsForValue().set("seckillPath:" + user.getId() + ":" + goodsId,path,60, TimeUnit.SECONDS);

        return path;
    }

    @Override
    public boolean checkPath(User user, Long goodsId, String path) {

        if(user == null || goodsId < 0 || !StringUtils.hasText(path)) {
            return false;
        }
        String redisPath =(String) redisTemplate.opsForValue().get("seckillPath:" + user.getId() + ":" + goodsId);
        return path.equals(redisPath);
    }

    @Override
    public boolean checkCaptcha(User user, Long goodsId, String captcha) {

        if(user == null || goodsId < 0 || !StringUtils.hasText(captcha)) {
            return false;
        }
        String redisCaptcha = (String)redisTemplate.opsForValue().get("captcha:" + user.getId() + ":" + goodsId);
        return captcha.equals(redisCaptcha);
    }
}
